﻿<script>
    var _charge_items =@this.RawSerialize(this.ViewBag.ChargeItemList);
    var _sorts = [{ Title: "消费", Id: 0 }, { Title: "充值", Id: 1 }, { Title: "作废", Id:2}];
    var _file_domain = "@ViewBag.FileDomain";
    var uploader;


    var _charges = new Array();
    _charge_items.map(function (item, i) {
        _charges.push({ Title: item.Remark, Id: i });
    });



    var _vm;
    $(function () {
        var vm = new PageViewModel();
        _vm = vm;
        ko.applyBindings(vm);
        vm.init();

    });
 

    function PageViewModel()
    {
        var me = this;
        ViewModel.call(me);
        vmExtend.call(me);

        me.url = "/Wiki/UsersCharge/Models";
        me.deleteUrl = "/Wiki/UsersCharge/Delete";

        
        me.dialog = new FormDialog(me);
         
    }
    function vmExtend() {
        var me = this;
    }


    function FormDialog(vm) {
        var me = this;
        Dialog.call(me);

        me.onOpen = function ()
        {

            if (!uploader)
            {
                uploader = WebUploader.create({
                    auto: true,
                    swf: '/Content/webuploader/Uploader.swf',
                    server: '/Wiki/Product/FileSave',
                    pick: '#picker',
                    resize: false
                });

                uploader.on('uploadSuccess', function (file, response) {
                    //$('#' + file.id).find('.progress').fadeOut();
                     

                    if (response.st == "1")
                    {
                        $("#ImageUrl").val(response.msg);
                        $("#thelist").append("<img class='img_thum img_red' src='" + _file_domain + response.msg + "' />");
                    }
                    else
                    {
                        $ace.alert(response.msg);
                    }


                });
            }

            var model = me.editModel();
            if (model)
            {
                var bindModel = $ko.toJS(model);
                me.model(bindModel);
                $(".modal-footer").find("button").hide();
                
                //console.log(bindModel);
                
                $("#ImageUrl").val(bindModel.ImageUrl);

                $("#thelist").empty();
                if (bindModel.ImageUrl != "")
                {
                    var s1 = $("#ImageUrl").val();
                    if (s1 != "")
                    {
                        $("#thelist").append("<img class='img_thum img_red' src='" + _file_domain + s1 + "' />");
                    }
                }
 
                if (bindModel.ChargeSort == 1)
                {
                    $(".charge_in").show();
                }
                else {
                    $(".charge_in").hide();
                }
                 
            }
            else
            {
                $(".modal-footer").find("button").show();
                
                $("#ChargeSort").attr("disabled","disabled").val("1");
                $("#ImageUrl").val("");
                $("#thelist").empty();
                $("#tr_code").hide();   
            }

           
            $("#UserCharge").change(function ()
            {
                var index = $("#UserCharge").val();
                var UserCharge = _charge_items[index].UserCharge;
                var ShopCharge = _charge_items[index].ShopCharge;
                var ChargeTotal = UserCharge + ShopCharge;
                 
                
                $("#ShopCharge").val(ShopCharge);
                $("#ChargeTotal").val(ChargeTotal);
            });
            //


            $('#btn_search').click(function () {
                var keyword1 = $("#keyword1").val();
                if (keyword1 != "") {
                    $ace.get("/Wiki/UsersCharge/GetModelByMobile", { Mobile: keyword1 }, function (result)
                    {
                        if (result.Status == 100)
                        {
                            //$("#SSZTMC").val(result.Data.SSZTMC);
                            //$("#Fid").val(result.Data.SHTYXYDM);
                            //me.model({ TrueName: result.Data.TrueName, Mobile: result.Data.Mobile, UserID: result.Data.Id});
                            $("#TrueName").val(result.Data.TrueName);
                            $("#Mobile").val(result.Data.Mobile);
                            $("#UserID").val(result.Data.Id);
                        }
                        else
                        {
                            $ace.alert(result.Msg);
                        }
                    });
                }
                else {
                    $ace.msg("请输入关键字");
                }
            });
        }

        me.onSave = function () {
            if (!$('#form1').formValid()) {
                return false;
            }

            var model = me.model();
            model.UserID = $("#UserID").val();
            model.ShopCharge = $("#ShopCharge").val();
            model.ChargeTotal = $("#ChargeTotal").val();
            model.UserCharge = $("#UserCharge").val();
            model.ImageUrl = $("#ImageUrl").val();
            model.ChargeSort = $("#ChargeSort").val();

            if (model.UserID == "" || model.UserID == undefined) {
                $ace.msg("请找到充值用户");
                return;
            }
            if (model.ChargeTotal == "" || model.ChargeTotal == 0 || model.ChargeTotal == undefined) {
                $ace.msg("请输入交易金额");
                return;
            }

            if (me.editModel())
            {
                model.ImageUrl = $("#ImageUrl").val();   
                $ace.post("/Wiki/UsersCharge/Edit", model, function (result) {
                    $ace.msg(result.Msg);
                    me.close();
                    vm.dataTable.reload();
                    //setTimeout(function () { window.location.reload(); }, 1000);
                }
                );
            }
            else {
                model.ImageUrl = $("#ImageUrl").val(); 
                $ace.post("/Wiki/UsersCharge/Add", model, function (result) {
                    $ace.msg(result.Msg);
                    me.close();
                    vm.loadData();
                    //setTimeout(function () { window.location.reload(); }, 1000);
                }
                );
            }
        }
    }

</script>
